// pages/evaluation/evaluation.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    evaluationList:[],
    imageList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //获取手机高度
    that.setData({
      navH: app.globalData.navHeight
    });
    that.setData({
      productid:options.productid
    });
    wx.request({
      url: app.globalData.url + 'getProductEvaluation',
      data:{productId: that.data.productid},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function(res){
        that.setData({
          evaluationList: res.data.data.reverse(),
        });
        for (var i = 0; i < that.data.evaluationList.length; i++) {
          that.data.evaluationList[i].reviewContent = that.entitiesToUtf16(that.data.evaluationList[i].reviewContent)
        }
        that.setData({
          evaluationList: that.data.evaluationList
        });
        var imgArr = [];
        for(var i = 0; i < res.data.data.length; i++){
          var evaImg = res.data.data[i].reviewImages;
          var arr1 = evaImg.split(';;;');
          if(arr1[arr1.length -1].trim() == ""){
            arr1.length = arr1.length - 1;
          }
          imgArr.push(arr1);
        }
        that.setData({
          imageList: imgArr,
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  navBack: function () {
    wx.navigateBack({

    })
  },
  //将编码后的八进制的emoji表情重新解码成十六进制的表情字符
  entitiesToUtf16: function (str) {
    return str.replace(/&#(\d+);/g, function (match, dec) {
      let H = Math.floor((dec - 0x10000) / 0x400) + 0xD800;
      let L = Math.floor(dec - 0x10000) % 0x400 + 0xDC00;
      return String.fromCharCode(H, L);
    });
  },
  previewImg: function (e) {
    console.log(e.currentTarget.dataset.index);
    var idx = e.currentTarget.dataset.idx;
    var index = e.currentTarget.dataset.index;
    var imgArr = this.data.imageList[idx];
    wx.previewImage({
      current: imgArr[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  }
})